
/*
________________________________________________________________________________
Extend the ownership data on incorporated entrepreneurs to early years, meaning years 1997-2005
*/

use "D:\d37\custom-made\shareholder_panel_1997_2016_muok.dta" , clear
keep if verovuosi<2006
ren verovuosi vuosi
bysort shnro vuosi: gen nr_of_firms = _N
egen NR_of_firms=max(nr_of_firms), by(shnro vuosi)
ren syrtun_maktun syrtun
keep syrtun shnro vuosi NR_of_firms
tab NR_of_firms
gen ie=1
gen ie_mltpl=0
replace ie_mltpl=1 if NR_of_firms>1
ren NR_of_firms nr_of_firms
keep shnro syrtun vuosi nr_of_firms ie ie_mltpl
label var nr_of_firms "Number of firms a person owns in a given year"
label var ie "Incorporated entrepreneur"
label var ie_mltpl "Incorporated entrepreneur who owns more than one firm"
keep shnro syrtun vuosi nr_of_firms ie ie_mltpl
gen yrtti=1
drop if shnro==""
append using "$data\99_temp\flownyrtti.dta"
replace yrtti=0 if yrtti==.
sort syrtun shnro vuosi
order syrtun shnro vuosi
egen firm_ind=group(syrtun shnro)
tsset firm_ind vuosi
tsfill

foreach var of varlist syrtun shnro nr_of_firms ie ie_mltpl yrtti{
bys firm_ind: carryforward `var', gen(`var'n) 
drop `var'
ren `var'n `var'
}
* keep the first firm that the owner has
sort shnro vuosi
bys shnro: gen eka=_n
keep if eka==1
drop firm_ind eka 
rename syrtun syrtun_ownrshp
save "$data\99_temp\flownyrtti.dta", replace // this is used in 01_05_starts, so do not erase yet

use "$data\01_main\folk_flown_merge.dta", clear

merge 1:1 shnro vuosi using "$data\99_temp\flownyrtti.dta"
drop if _merge==2
drop _merge
order shnro-ie_mltpl

foreach var of varlist nr_of_firms ie ie_mltpl {
	replace `var'=0 if `var'==.
}
order shnro vuosi nr_of_firms ie ie_mltpl
tab vuosi

save "$data\01_main\folk_flown_merge.dta", replace

/*
________________________________________________________________________________
Identify unincorporated entrepreneurs
*/

use "D:\d37\custom-made\yhtymaosakaspaneeli_1996_2016.dta", clear
rename verovuosi vuosi
keep shnro vuosi // keep liikevaihto tms.
append using "W:\Inequality\Data\toiminimipaneeli.dta", keep(vuosi shnro)
sort shnro vuosi
drop if shnro==""
*drop something
gen uie=1
collapse uie, by(shnro vuosi)
label var uie "Unincorporated entrepreneur"
save "$data\99_temp\uietags.dta", replace

use "$data\01_main\folk_flown_merge.dta", clear
merge 1:1 shnro vuosi using "$data\99_temp\uietags.dta"
drop if _merge==2
drop _merge
erase "$data\99_temp\uietags.dta"
order shnro-ie_mltpl uie
replace uie=0 if uie==.

/*
Refine the definition of unincorporated entrepreneurs:
A person is WE if they earn nothing from the unincorporated business or are not main owners
*/

* Has to be main owner, only applies to partnerships
preserve
use "D:\d37\custom-made\yhtymaosakaspaneeli_1996_2016.dta", clear
rename verovuosi vuosi
drop if shnro==""
keep shnro vuosi syrtun yhstulotpot yhstulotat
drop if yhstulotpot==. & yhstulotat==.
replace yhstulotpot=0 if yhstulotpot==.
replace yhstulotat=0 if yhstulotat==.
gen uie_income=yhstulotpot+yhstulotat
drop if yhstulotpot==0 & yhstulotat==0
keep vuosi shnro syrtun uie_income
bysort syrtun vuosi: egen max_uie_income = max(uie_income)
gen uie_income_rnd=round(uie_income, 0.01)
gen max_uie_income_rnd=round(max_uie_income, 0.01)
gen likely_mainowner=0
replace likely_mainowner=1 if uie_income_rnd==max_uie_income_rnd
keep if likely_mainowner==0
keep vuosi shnro
gen not_uie=1
duplicates drop
save "$data\99_temp\not_uie.dta", replace
restore
merge 1:1 shnro vuosi using "$data\99_temp\not_uie.dta"
drop if _merge==2
drop _merge
replace uie=0 if not_uie==1
drop not_uie
erase "$data\99_temp\not_uie.dta"

* Define that the sole proprietors are mainly self-employed and not wage earners
preserve
use shnro vuosi liikevaihto using "W:\Inequality\Data\toiminimipaneeli.dta", clear
replace liikevaihto=0 if liikevaihto==.
drop if liikevaihto==0
save "$data\99_temp\soleprop_turnover.dta", replace
restore
merge 1:1 shnro vuosi using "$data\99_temp\soleprop_turnover.dta"
drop if _merge==2
drop _merge
replace liikevaihto=0 if liikevaihto==.
preserve
use vuosi shnro telinko if vuosi>=1997 using "D:\ready-made\FOLK_tulo_8800a\folk_19872000_tua_tulo21tot_1.dta", clear
append using "D:\ready-made\FOLK_tulo_0110a\folk_20012010_tua_tulo21tot_1.dta", keep(vuosi shnro telinko)
append using "D:\ready-made\FOLK_tulo_11a\folk_20112019_tua_tulo21tot_1.dta", keep(vuosi shnro telinko)
drop if vuosi>2014
drop if telinko==.
save "$data\99_temp\soleprop_income.dta", replace
restore
merge 1:1 shnro vuosi using "$data\99_temp\soleprop_income.dta"
drop if _merge==2
drop _merge
replace telinko=0 if telinko==.
gen shr_e_income_soleprop = telinko / mkti
replace uie=0 if liikevaihto==0 // has to have positive turnover
replace uie=0 if shr_e_income_soleprop<0.5 // has to get at least half of their income as entrepreneurial income
drop liikevaihto telinko shr_e_income_soleprop
erase "$data\99_temp\soleprop_turnover.dta"
erase "$data\99_temp\soleprop_income.dta"

/*
________________________________________________________________________________
Labels etc.
*/

label var syrtun_ownrshp "Id of the owned firm"
label var yrtti "Source tag: 1 is Yrtti, 0 is FLOWN"
save "$data\01_main\folk_flown_merge.dta", replace
clear